﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using MM.Model;
using MM.DBUtility;
using MM.Common;

namespace WebMMS
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Json解析
                //string strJosn = "{\"charge\":{\"money\":100,\"appid\":101,\"chargeid\":1,\"mode\":1},\"info\":{\"sim\":{\"plmn\":\"46000\",\"number\":\"15201877668\",\"imsi\":\"460022018077374\",\"imei\":\"358920045683155\"},\"phone\":{\"position\":2,\"pid\":9999,\"mac\":\"18:87:96:B3:B1:43\",\"height\":800,\"width\":480,\"name\":\"HTC Desire HD A9191\",\"version\":\"2.3.5\"},\"version\":{\"code\":310}}}";

                string strJosn = "{\"szf\":{\"card\":\"100100100100100100100100\",\"pwd\":\"101101101101101101101\",\"price\":50,\"op\":1},\"charge\":{\"money\":100,\"appid\":101,\"chargeid\":1,\"mode\":2,\"matchid\":1,\"gametype\":1},\"info\":{\"sim\":{\"plmn\":\"46000\",\"number\":\"15201877668\",\"imsi\":\"460022018077374\",\"imei\":\"358920045683155\"},\"phone\":{\"position\":2,\"pid\":9999,\"mac\":\"18:87:96:B3:B1:43\",\"height\":800,\"width\":480,\"name\":\"HTC Desire HD A9191\",\"version\":\"2.3.5\"},\"version\":{\"code\":310}}}";

                var Result = JsonConvert.DeserializeObject<JsonResultInfoSZF>(strJosn);
                simInfo sim = Result.info.sim;
                phoneInfo phone = Result.info.phone;
                versionInfo version = Result.info.version;
                chargeInfo charge = Result.charge;
                szfInfo szf = Result.szf;

                string strOrderNo = string.Empty;
                using (SqlConnection conn = new SqlConnection(DbHelperSql.connectionString))
                {
                    SqlParameter[] parameters = { 
                       new SqlParameter("@imsimac", SqlDbType.VarChar),
                       new SqlParameter("@netType", SqlDbType.Int),
                       new SqlParameter("@chargeMode", SqlDbType.Int),
                       new SqlParameter("@appID", SqlDbType.Int),
                       new SqlParameter("@prizeID", SqlDbType.Int),
                       new SqlParameter("@gameType", SqlDbType.Int),
                       new SqlParameter("@taskID", SqlDbType.Int),
                       new SqlParameter("@price", SqlDbType.Decimal),
                       new SqlParameter("@card", SqlDbType.VarChar),
                       new SqlParameter("@pwd", SqlDbType.VarChar),
                       new SqlParameter("@szfPrice", SqlDbType.Decimal),
                       new SqlParameter("@op", SqlDbType.Int)};
                    parameters[0].Value = sim.imsi.ToStringOr() + phone.mac.ToStringOr();
                    parameters[1].Value = 0;
                    parameters[2].Value = charge.mode.ToIntOr();
                    //parameters[3].Value = charge.appid.ToIntOr();
                    parameters[4].Value = 0;
                    //parameters[5].Value = charge.gametype.ToIntOr();
                    //parameters[6].Value = charge.matchid.ToIntOr();
                    parameters[7].Value = charge.money.ToDecimalOr();
                    parameters[8].Value = szf.card.ToStringOr();
                    parameters[9].Value = szf.pwd.ToStringOr();
                    parameters[10].Value = szf.price.ToDecimalOr();
                    parameters[11].Value = szf.op.ToIntOr();

                    using (SqlDataReader dr = DbHelperSql.ExecuteReaderSP(conn, "AddOrderSzf", parameters))
                    {
                        if (dr != null && dr.HasRows)
                        {
                            //if (dr.Read())
                            //{
                            //    object obj = dr.GetValue(0);
                            //    strOrderNo = dr["orderNo"].ToStringOr();
                            //}
                            List<OrderInfo> list = new List<OrderInfo>();
                            while (dr.Read())
                            {
                                OrderInfo model = DTOHelper.New<OrderInfo>();
                                model.LoadData(dr);
                                list.Add(model);
                            }
                        }
                        dr.Close();
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
    public class OrderInfo : IReaderLoad
    {
        public void LoadData(IDataReader dr)
        {
            this.orderNo = Convert.ToString(dr["orderNo"]);
            this.a = Convert.ToString(dr["a"]);
            this.b = Convert.ToString(dr["b"]);
        }

        public string orderNo { get; set; }
        public string a { get; set; }
        public string b { get; set; }
    }
    public interface IReaderLoad
    {
        void LoadData(IDataReader dr);
    }
}